from typing import List, Optional, Dict
from uuid import UUID
from datetime import datetime
from pydantic import BaseModel, Field

from schemas.document.document_category import DocumentCategoryResponse
from schemas.document.document_tag import DocumentTagResponse
from schemas.document.document_compliance_relation import DocumentComplianceRelationResponse


class PersonalDocumentBase(BaseModel):
    """个人文档基础模型"""
    title: str = Field(..., description="文档标题", max_length=255)
    description: Optional[str] = Field(None, description="文档描述")
    category_id: Optional[UUID] = Field(None, description="所属分类ID")


class PersonalDocumentCreate(PersonalDocumentBase):
    """创建个人文档请求模型"""
    tags: Optional[List[str]] = Field(default=[], description="标签列表")


class PersonalDocumentUpdate(BaseModel):
    """更新个人文档请求模型"""
    title: Optional[str] = Field(None, description="文档标题", max_length=255)
    description: Optional[str] = Field(None, description="文档描述")
    category_id: Optional[UUID] = Field(None, description="所属分类ID")
    tags: Optional[List[str]] = Field(None, description="标签列表")


class PersonalDocumentResponse(PersonalDocumentBase):
    """个人文档响应模型"""
    id: UUID
    filename: str = Field(description="原始文件名")
    file_size: int = Field(description="文件大小(字节)")
    file_type: str = Field(description="文件类型")
    mime_type: Optional[str] = Field(description="MIME类型")
    is_favorite: bool = Field(description="是否收藏")
    is_pinned: bool = Field(description="是否置顶")
    view_count: int = Field(description="查看次数")
    created_at: datetime
    updated_at: datetime
    last_accessed_at: Optional[datetime] = Field(description="最后访问时间")
    
    # 关联对象
    category: Optional[DocumentCategoryResponse] = Field(description="所属分类")
    tags: List[DocumentTagResponse] = Field(default=[], description="标签列表")

    class Config:
        from_attributes = True


class PersonalDocumentDetailResponse(PersonalDocumentResponse):
    """个人文档详情响应模型"""
    compliance_relations: List[DocumentComplianceRelationResponse] = Field(default=[], description="关联的检查项")

    class Config:
        from_attributes = True


class PaginatedDocumentResponse(BaseModel):
    """分页文档响应模型"""
    items: List[PersonalDocumentResponse]
    total: int = Field(description="总数量")
    page: int = Field(description="当前页码")
    size: int = Field(description="每页数量")
    pages: int = Field(description="总页数")


class DocumentSearchRequest(BaseModel):
    """文档搜索请求模型"""
    category_id: Optional[UUID] = Field(None, description="分类ID筛选")
    search: Optional[str] = Field(None, description="搜索关键词")
    tags: Optional[List[str]] = Field(None, description="标签筛选")
    is_favorite: Optional[bool] = Field(None, description="是否收藏筛选")
    is_pinned: Optional[bool] = Field(None, description="是否置顶筛选")
    sort_by: str = Field(default="created_at", description="排序字段")
    sort_order: str = Field(default="desc", description="排序方向: asc/desc")
    page: int = Field(default=1, description="页码", ge=1)
    size: int = Field(default=20, description="每页数量", ge=1, le=100)


class DocumentUploadResponse(BaseModel):
    """文档上传响应模型"""
    document: PersonalDocumentResponse
    message: str = Field(description="上传结果消息")


class BatchOperationRequest(BaseModel):
    """批量操作请求模型"""
    document_ids: List[UUID] = Field(..., description="文档ID列表", min_items=1)


class BatchMoveRequest(BatchOperationRequest):
    """批量移动请求模型"""
    category_id: Optional[UUID] = Field(None, description="目标分类ID")


class DocumentStatistics(BaseModel):
    """文档统计信息"""
    total: int = Field(description="总文档数")
    favorites: int = Field(description="收藏文档数")
    by_type: Dict[str, int] = Field(description="按文件类型统计")
    by_category: Dict[str, int] = Field(description="按分类统计")
    total_size: int = Field(description="总文件大小(字节)")


class FileTypeInfo(BaseModel):
    """文件类型信息"""
    extension: str = Field(description="文件扩展名")
    icon: str = Field(description="文件类型图标")
    color: str = Field(description="文件类型颜色")
    name: str = Field(description="文件类型名称")
